package com.zac.coroutine.algorithm.leetcode.others

/**
 * author zac
 * date 2025/8/4
 *
 */
object VersionControl {
    @JvmStatic
    fun main(args: Array<String>) {
        print(firstBadVersion(1501))
    }

    fun firstBadVersion(n: Int): Int {
        if (n == 1) return 1
        var max = n
        var min = 0
        var cur = n
        while (true) {
            val curIsBad = isBadVersion(cur)
            if (curIsBad) {
                if (!isBadVersion(cur - 1)) return cur
                else {
                    max = cur - 1
                    cur = (cur - 1) / 2
                }
            } else {
                if (isBadVersion(cur + 1)) return cur + 1
                else {
                    min = cur + 1
                    cur = (max - min) / 2
                }
            }
        }
    }

    fun isBadVersion(version: Int): Boolean {
        return version >= 454
    }

}